//
//  NewsList.swift
//  newsApp
//
//  Created by Doo Psyist on 2020/5/12.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import SwiftUI

struct Response: Codable {
    var newsListItemState: [NewsListItem]
}
class NewsModel: ObservableObject { //dynamically load news
    @Published var newsListItems: [NewsListItem] = []
    func load() {
        self.newsListItems = newsList
        //loadData()
    }
}
struct NewsList: View {
    init() {
        //修改导航栏文字颜色
        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.systemOrange]
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.systemOrange]
        UINavigationBar.appearance().tintColor = .systemOrange

    }

    // With backend
    // @State var newsListItemState = [NewsListItem]()

    // Without backend (local debug/demo)
    @ObservedObject var newsListItemState = NewsModel()
    @State var initone = 0;
    var body: some View {
        NavigationView {
            List {
                ForEach(newsListItemState.newsListItems) { newsListItemStateOne in
                    NavigationLink(destination: NewsDetail(commentArray: loadComment(id: newsListItemStateOne.id), newsListItem: newsListItemStateOne)) {
                        NewsRow(newsListItem: newsListItemStateOne)
                    }
                }
            }
            // With backend
            //.onAppear(perform: initDataincoredata)
            //.onDisappear()
            .navigationBarTitle(Text("News"))
                .onAppear() {
                    self.initone = self.initone + 1
                    if self.initone == 1 {
                        initDataincoredata()
                        self.newsListItemState.load() //local data loading. if you want to load backend data, pls modify initDataincoredata() in CoreDataHelper.
                    } else if self.initone > 1 {
                        self.loadData() // backend load data
                    }
            }

        }



    }



    func delete(at offsets: IndexSet) {
        newsListItemState.newsListItems.remove(atOffsets: offsets)
    }

    func loadData() {
        guard let url = URL(string: "http://127.0.0.1:5000/get_list") else {
            print("invalid URL")
            return
        }

        let request = URLRequest(url: url)

        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                    DispatchQueue.main.async {
                        print(data)
                        self.newsListItemState.newsListItems = decodedResponse.newsListItemState
                        networkJson = decodedResponse.newsListItemState
                    }
                }
            }
        }.resume()
    }

}





struct NewsList_Previews: PreviewProvider {
    static var previews: some View {
        NewsList()
    }
}

